//
//  Timer.swift
//  TestProject
//
//  Created by 万想想 on 2020/8/19.
//  Copyright © 2020 edaili. All rights reserved.
//

import UIKit

class Timer {
    static let shareInstance = Timer()
    
    let sourceTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
    typealias timeCountHandler = (() -> ())
    
    var timerCount = 0
    
    var timerIsCancel: Bool{
        didSet{
            timerIsCancel ? sourceTimer.resume() : sourceTimer.suspend()
        }
    }
    
    private init() {
        timerIsCancel = false
    }
    
    func setEventHandler(completion: @escaping timeCountHandler){
        sourceTimer.schedule(deadline: .now() + 5, repeating: .seconds(5))
        sourceTimer.setEventHandler(handler: {
            completion()
        })
        
    }
    func timerCancel(){
        Timer.shareInstance.timerIsCancel = false
        Timer.shareInstance.timerCount = 0
    }
}
